from database import User_DB
import hashlib


def regist(user_name: str, password: str):
    """
    根据用户名和密码注册用户，即将用户名和对应md5加密结果写入数据库
    :param user_name:
    :param password:
    :return:
    """
    user_name_list = "".join(user_name.split())
    password_list = "".join(password.split())
    m = hashlib.md5()
    m.update(user_name_list.encode("utf-8"))
    m.update(password_list.encode("utf-8"))
    # md5加密结果
    cryp = m.hexdigest()
    reg_command = f"REPLACE INTO user_info VALUES({user_name}, '{cryp}');"
    try:
        user_db = User_DB()
        user_db.exeNonQuery(reg_command)
        print(f"-----{user_name} regists successfully-----")
        return True
    except Exception as e:
        print(f"-----{user_name} regists failed, {e}-----")
        return False

regist("1940153978", "ztc020517517ztc")
